<?php
namespace zake\foundation\local;

/**
 * Class File
 * @package App\Libs\Fsc
 *
 * @author liu.bin 2019-07-05 11:07
 */
class File extends Local
{
    /**
     * 创建file
     * @param $localPath
     * @return File|static
     * @author liu.bin 2020-05-07 10:15
     */
    public static function create($localPath)
    {
        return new static($localPath);
    }


    /**
     * 不存在则创建本地文件
     * @param string $localPath
     * @return File|static
     * @author liu.bin 2020-05-07 10:19
     */
    public static function createOrNew($localPath)
    {
        $file = new static($localPath);
        if(!$file->exists()){
            $file->mkFile();
        }
        return $file;
    }



    /**
     * @var array
     */
    private $pathInfo = [];


    /**
     * @param string $filePath 文件全路径
     * File constructor.
     */
    public function __construct($localPath)
    {
        parent::__construct($localPath);
        $this->pathInfo = pathinfo($localPath);
    }


    /**
     * 创建一个新文件
     *
     * @author liu.bin 2020-05-07 10:25
     */
    public function mkFile()
    {
        $this->getResource();
        $this->closeResource();
    }



    /**
     * 带扩展名的文件名称
     * @return string
     *
     * @author liu.bin 2020-05-06 23:17
     */
    public function getFileFullName()
    {
        return $this->pathInfo['basename'] ?? '';
    }


    /**
     * 不带扩展名的文件名称
     * @return string
     *
     * @author liu.bin 2020-05-06 23:17
     */
    public function getFileName()
    {
        return $this->pathInfo['filename'] ?? '';
    }


    /**
     * 获取文件目录
     * @return string
     *
     * @author liu.bin 2020-05-06 23:19
     */
    public function getFilePath()
    {
        return $this->pathInfo['dirname'] ?? $this->getLocalPath();
    }


    /**
     * 扩展名
     * @return string
     *
     * @author liu.bin 2020-05-06 23:20
     */
    public function getFileExt()
    {
        return $this->pathInfo['extension'] ?? '';
    }

    /**
     * 文件大小 单位：B
     * @return int
     *
     * @author liu.bin 2020-05-06 23:20
     */
    public function getSize()
    {
        return filesize($this->getLocalPath());
    }


    /**
     * 文件md5
     * @return string
     *
     * @author liu.bin 2020-05-07 08:43
     */
    public function getMd5()
    {
        if(!$this->isFile()){
            return "";
        }
        return md5_file($this->getLocalPath());
    }

    /**
     * 文件sha1
     * @return string
     *
     * @author liu.bin 2020-05-07 08:44
     */
    public function getSha1()
    {
        if(!$this->isFile()){
            return "";
        }
        return sha1_file($this->getLocalPath());
    }


    /**
     * 对象转map数据
     * @return array
     *
     * @author liu.bin 2019-07-16 09:48
     */
    public function toMap()
    {
        $dummy = [];
        $dummy['name']      = $this->getFileFullName();
        $dummy['path']      = $this->getFilePath();
        $dummy['ext']       = $this->getFileExt();
        $dummy['md5']       = $this->getMd5();
        $dummy['sha1']      = $this->getSha1();
        return $dummy;
    }

}
